package com.wanwanbear.www.service.impl;

import com.wanwanbear.www.mapper.UserMapper;
import com.wanwanbear.www.netty.NettyServer;
import com.wanwanbear.www.pojo.NewResult;
import com.wanwanbear.www.pojo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wanwanbear.www.pojo.User;
import com.wanwanbear.www.service.FindUser;
import org.springframework.web.bind.annotation.RequestParam;
import sun.net.www.http.HttpClient;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by wz on 2017/6/6.
 */
@Service
public class ImplFindUser implements FindUser {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NettyServer nettyServer;

    /**
     * 方   法  名:selectByIdFindUser
     * 创 建 日 期:2017/9/18
     * 创  建   者:zhang.zhipeng
     * 描       述:根据ID查询用户
     * 参       数:[id]
     * 返  回   值:com.wanwanbear.www.pojo.User
     */
    public User selectByIdFindUser(int id) {
        User user;
        user = userMapper.selectByPrimaryKey(id);
/*        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<User> list = userMapper.selectByExample(userExample);
        user = list.get(0);*/
        return user;
    }

    public List<User> getOnCriteria(@RequestParam(defaultValue = "0") int before,@RequestParam(defaultValue = "1") int end){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andAgeBetween(before,end);
        User user = new User();
        user.setAge(33);
        user.setUsername("中文");
        userMapper.insert(user);
        List<User> list = userMapper.selectByExample(userExample);
        return list;
    }

    public User getItem(int id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }
    public NewResult insertUser(String username, int age){
        User user = new User();
        user.setUsername(username);
        user.setAge(age);
        userMapper.insert(user);
        NewResult newResult = new NewResult();
        newResult.build(200,"成功");
        newResult.setData(null);
        return newResult;
    }
    public NewResult userNetty() {
        try {
            nettyServer.run();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        NewResult newResult = new NewResult();
        newResult.build(200,"成功");
        newResult.setData(null);
        return newResult;
    }
}
